﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace Dao
{
    public class KC_Dao
    {
        /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(KC entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.KCSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(KC entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                KC entity2 = context.KCSet.FirstOrDefault(c => c.ID == entity.ID);
                if (entity2 != null)
                {
                    context.KCSet.ApplyCurrentValues(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                KC entity = context.KCSet.FirstOrDefault(c => c.ID == guid);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public KC Query(string id)
        {
            using (var context = new Dao.ErpEntities())
            {
                Guid guid = Guid.Parse(id);
                KC entity = context.KCSet.FirstOrDefault(c => c.ID == guid);
                return entity;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<KC> QueryBySql(string sql)
        {
            List<KC> list = new List<KC>();
            using (var context = new Dao.ErpEntities())
            {
                ObjectQuery<KC> q = context.CreateQuery<KC>(sql);
                ObjectResult<KC> results = q.Execute(MergeOption.NoTracking);
                foreach (KC c in results)
                {
                    list.Add(c);
                }

                return list;
            }
        }

        public List<KC> QueryBySql(string lx, string wpid, string ckid, string cwid)
        {
            List<KC> list = new List<KC>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.KCSet
                        where c.WPLX == lx
                        select c;

                if (!string.IsNullOrEmpty(wpid) && (!"0".Equals(wpid)))
                {
                    Guid wid = Guid.Parse(wpid);
                    q = q.Where(c => c.WPID == wid);
                }

                if (!string.IsNullOrEmpty(ckid) && (!"0".Equals(ckid)))
                {
                    Guid cid = Guid.Parse(ckid);
                    q = q.Where(c => c.CKID == cid);
                }

                if (!string.IsNullOrEmpty(cwid) && (!"0".Equals(cwid)))
                {
                    Guid cwGuid = Guid.Parse(cwid);
                    q = q.Where(c => c.CWID == cwGuid);
                }

                foreach (KC c in q)
                {
                    list.Add(c);
                }

                return list;
            }
        }

        public List<vWLKC> QueryWLBySql(string lx, string lxbh, string gysid, string wpmc, string ckid, string cwid)
        {
            List<vWLKC> list = new List<vWLKC>();
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.vWLKCSet
                        //where c.LXBH == lx
                        select c;

                if (!string.IsNullOrEmpty(lxbh) && (!"0".Equals(lxbh)))
                {
                    q = q.Where(c => c.LXBH == lxbh);
                }
                if (!string.IsNullOrEmpty(gysid) && (!"0".Equals(gysid)))
                {
                    Guid gid = Guid.Parse(gysid);
                    q = q.Where(c => c.GYSID == gid);
                }
                if (!string.IsNullOrEmpty(ckid) && (!"0".Equals(ckid)))
                {
                    Guid cid = Guid.Parse(ckid);
                    q = q.Where(c => c.CKID == cid);
                }
                if(!string.IsNullOrEmpty(wpmc))
                {
                    q = q.Where(c => c.MC.Contains(wpmc));
                }
                if (!string.IsNullOrEmpty(cwid) && (!"0".Equals(cwid)))
                {
                    Guid cwGuid = Guid.Parse(cwid);
                    q = q.Where(c => c.CWID == cwGuid);
                }

                foreach (vWLKC c in q.OrderBy(c=>c.BH))
                {
                    list.Add(c);
                }

                return list;
            }
        }

        public bool IsCreateKC(string id)
        {
            Guid gid = Guid.Parse(id);
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.vWLKCSet
                        where c.WPID == gid
                        select c;
                return q.Count() > 0;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<KC> QueryAll()
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.KCSet
                        select c;

                List<KC> list = new List<KC>();
                if (q.Count() > 0)
                {
                    foreach (var c in q)
                    {
                        list.Add(c);
                    }
                }
                return list;
            }
        }
    }
}
